﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmRepairList
    Public Primary_Key As Integer = -1
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
#Region "Functioin"
    Sub Load_Data()
        Try
            Strsql = "Select repair_order_id,repair_order_no,repair_budget_year,repair_order_date,repair_department,repiar_staff,repair_finish_date,repair_place,repair_status"
            Strsql = Strsql & " ,dep_name,concat(employee.emp_fname,employee.emp_lname) as fullname,repair_status_name,repair_place_name"
            Strsql = Strsql & " from repair_order "
            Strsql = Strsql & " left join departments on repair_order.repair_department = departments.dep_id"
            Strsql = Strsql & " left join employee on repair_order.repiar_staff = employee.emp_id"
            Strsql = Strsql & " left join repair_place on repair_order.repair_place = repair_place.repair_place_id"
            Strsql = Strsql & " left join repair_status on repair_order.repair_status = repair_status.repair_status_id"
            Dim DT_Doc_Draw_Header As DataTable
            DT_Doc_Draw_Header = _mysql.GetMYSQLDataTable(Strsql, "repair_order")
            With GridControl1
                .DataSource = DT_Doc_Draw_Header
                'Col_doc_id.FieldName = "repair_order_id"
                If DT_Doc_Draw_Header.Rows.Count = 0 Then
                    BtnDelete.Enabled = False
                    BtnEdit.Enabled = False
                Else
                    BtnEdit.Enabled = True
                    BtnDelete.Enabled = True
                End If
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
   
#End Region

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        With frmNewRepairForm
            '.MdiParent = frmmain
            '.WindowState = FormWindowState.Maximized
            If .ShowDialog <> Windows.Forms.DialogResult.OK Then Exit Sub

            Load_Data()

        End With
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub frmRepairList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Data()
    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try
            Dim Gv As GridView = sender
            Primary_Key = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("repair_order_id"))

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub GridControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridControl1.Click

    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        If Primary_Key = -1 Then Exit Sub
        With frmEditRepairForm
            .Primary_Key = Primary_Key
            .MdiParent = frmmain
            .WindowState = FormWindowState.Maximized
            .Show()
            Me.Close()
        End With
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click

        If Primary_Key = -1 Then
            MsgBox("กรุณาเลือก Record ที่ต้องการจะลบ", MsgBoxStyle.Exclamation, "คำเตือน")
            Exit Sub
        End If
        If MsgBox("คุณต้องการลบข้อมูล 'การซ่อม' รหัส " & Primary_Key & " ออกจากระบบหรือไม่?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub




        Try
            Strsql = "delete from repair_detail where repair_order_id = " & Primary_Key
            If _mysql.MySQLExecute(Strsql) < 0 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnDelete_Click : delete detail"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With


            End If
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnDelete_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

        Try
            Strsql = "delete from repair_order where repair_order_id = " & Primary_Key
            If _mysql.MySQLExecute(Strsql) < 0 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnDelete_Click : delete header"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With
            Else
                MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบ")
                Load_Data()
            End If

            Primary_Key = -1
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnDelete_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


    End Sub

    Private Sub BtnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRefresh.Click
        Load_Data()

    End Sub
End Class